/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simple.utils.httpclient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;

/**
 *
 * @author rehak
 */
public class AcceptAllSSLContextFactory {

    private static final Logger LOGGER = LoggerFactory.getLogger(AcceptAllSSLContextFactory.class);
    
    public static SSLContext getSSLContext() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[]{new KeyManager() {
            }}, new TrustManager[]{new X509TrustManager() {

                @Override
                public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
                    LOGGER.debug("Client trusted: "+string+", "+Arrays.toString(xcs));
                }

                @Override
                public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
                    LOGGER.debug("Server trusted: "+string+", "+Arrays.toString(xcs));
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            }}, new SecureRandom());        
        return ctx;
    }
}
